package com.dc.project.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.vo.ResultVo;
import com.dc.project.workflow.dao.WfCategoryDao;
import com.dc.project.workflow.dto.WfCategoryDto;
import com.dc.project.workflow.entity.WfCategory;
import com.dc.project.workflow.service.WfCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 流程分类表(WfModelCategory)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-21 15:50:26
 */
@Service("wfCategoryService")
public class WfCategoryServiceImpl extends ServiceImpl<WfCategoryDao, WfCategory> implements WfCategoryService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 查询流程分类表列表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByList(WfCategoryDto dto) {
        QueryWrapper<WfCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(WfCategory::getSortNum);
        List<WfCategory> list = this.list(queryWrapper);
        return ResultVo.success().data(list);
    }

    /**
     * 新增流程分类表
     *
     * @param wfCategory
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(WfCategory wfCategory) {
        if (null == wfCategory.getParentId()) {
            wfCategory.setParentId(0);
        }
        return ResultVo.verify(this.save(wfCategory));
    }

    /**
     * 编辑流程分类表
     *
     * @param wfCategory
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(WfCategory wfCategory) {
        if (null == wfCategory.getParentId()) {
            wfCategory.setParentId(0);
        }
        return ResultVo.verify(this.updateById(wfCategory));
    }

    /**
     * 根据主键删除流程分类表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 查询流程分类关联信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResultVo getModelCategory(WfCategoryDto dto) {
        return null;
    }
}